<template>
  <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
    <el-form @submit.native.prevent :model="type" :rules="rules" ref="typeForm" label-width="100px">
      <el-row style="display: flex;justify-content: center;">
        <el-col :span="12" style="width: 100%;">
          <el-form-item label="宠物类型:" prop="typeName">
            <el-input v-model="type.typeName" placeholder="请输入宠物类型"  size="small" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
          <el-button @click="cancel" size="small">取 消</el-button>
          <el-button type="primary" @click="saveOrEdit" size="small">确 定</el-button>
        </span>
  </el-dialog>
</template>

<script>
import {Message} from "element-ui";

export default {
  name: "ActivityOper",
  data() {
    return {
      title: '',
      dialogVisible: false,
      callbackImageView: true,
      operationType: 0,//0添加 1编辑
      type: {
        id: "",
        typeName: ""
      },
      rules: {
        typeName: [{required: true, message: "请输入宠物类型", trigger: 'blur'}],
      },
    };
  },
  methods: {
    clearObj(obj) {
      Object.keys(obj).forEach(key => {
        obj[key] = "";
      });
    },
    closeView() {
      this.dialogVisible = false;
      this.$parent.initData();
    },
    cancel() {
      this.closeView();
    },
    saveOrEdit() {
      this.$refs['typeForm'].validate(validate => {
        if (validate) {
          if (this.operationType === 0) {
            this.save();
          } else {
            this.edit();
          }
        }
      });
    },
    save() {
      this.postRequest("/pet/type-dictionary/insertTypeDictionary", this.type).then(resp => {
        if (resp.code === 200) {
          this.$message({
            type: 'success',
            message: '添加成功!'
          });
          this.closeView();

        } else {
          this.$message({
            type: 'error',
            message: '添加失败!'
          });
        }
      });
    },
    edit() {
      this.putRequest("/pet/type-dictionary/updateTypeDictionary", this.type).then(resp => {
        if (resp.code === 200) {
          this.$message({
            type: 'success',
            message: '修改成功!'
          });
          this.closeView();

        } else {
          this.$message({
            type: 'error',
            message: '修改失败!'
          });
        }
      });
    }
  }
}
</script>

<style scoped>
  .upload {
    border: 1px dotted #dedede;
    margin-top: 15px;
    display: none;
    width: 200px;
    height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>
